package com.gsdse.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gsdse.pojo.ApplyExcel;
import com.gsdse.pojo.ApplyTime;
import com.gsdse.pojo.apply;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ApplyDao extends BaseMapper<apply> {

    //查找用户是否有提交记录
    @Select("select * from apply where userId=#{userId}")
    apply selectApplybyUserid(Integer userId);

    //审核更改志愿状态 OR 二次投递覆盖原数据
    @Update("update apply set pdfUrl=#{pdfUrl},time=#{time} ,firstWilling=#{firstWilling} ,secondWilling= #{secondWilling} , firstStatus = #{firstStatus} , secondStatus= #{secondStatus} where userId=#{userId}")
    Boolean updateApply(apply apply);

    //创建一条apply记录
    @Insert("insert into apply values (#{applyId},#{time},#{pdfUrl},#{firstWilling},#{secondWilling},null,null,#{userId})")
    Boolean insertApply(apply apply);


    //查找用户是否有提交记录
    @Select("select * from apply where applyId = #{applyId}")
    apply selectApplyByApplyId(Integer applyId);


    //根据willing查apply
    @Select("select * from apply where firstWilling=#{department} or secondWilling=#{department}")
    List<apply> selectApplyByWilling( String department);



    //根据willing查apply,查询部门报名人数
    @Select("SELECT COUNT(*) FROM apply WHERE (time<=#{endTime} and time>=#{startTime}) and  (firstWilling = #{department} or secondWilling = #{department})")
    Integer selectCountByFirstWilling(@Param("department") String department, @Param("startTime") String startTime, @Param("endTime") String endTime);


    //查询两个志愿都通过的
    @Select("select * from apply where firstWilling=1 and secondWilling=1")
    List<apply> selectApplyBytwoWilling();


    //根据时间戳找apply
    @Select("select * from apply where time=#{timestamp}")
    apply selectApplyBytimestamp(long timestamp);


    @Insert("insert into apply (applyId,userId) values(#{applyId},#{userId})")
    Boolean SetUserId(String applyId,String userId);


    @Update("update apply set firstStatus=#{firstStatus},secondStatus=#{secondStatus} where applyId=#{applyId}")
    Boolean reverseWilling(@Param("applyId") int applyId, @Param("firstStatus") String firstStatus,@Param("secondStatus") String secondStatus);


    @Select("select * from apply WHERE (time<=#{endTime} and time>=#{startTime}) and  (firstWilling = #{departmentId} or secondWilling = #{departmentId}) order by time")
    List<apply> selectApplyByWillingAndTime(@Param("departmentId") String departmentId, @Param("startTime") String startTime, @Param("endTime") String endTime);

    @Select("select * from applytime where id=1")
    ApplyTime getApplyTime();

    @Update("update applytime set startTime=#{startTime},endTime=#{endTime} where id=1")
    void setApplyTime(@Param("startTime") String startTime,@Param("endTime") String endTime);

    @Select("select user.userName,user.schoolClass,user.email,user.phoneNumber,apply.pdfUrl,apply.time from user,apply where (time<=#{endTime} and time>=#{startTime}) and user.userId=apply.userId and (apply.firstWilling=#{deptId} || apply.secondWilling=#{deptId}) order by time")
    List<ApplyExcel> getDepartmentApplyExcel(@Param("deptId") String deptId,@Param("startTime")String startTime,@Param("endTime") String endTime);
}

